import ika
from map_entity import MapEntity
from enemy import Enemy
import map
import direction
import engine
import resources

RUN_RECOVERY_TIME = 100
RUN_MAX_DURATION = 300
ATTACK_DURATION = 30

SWORD_WIDTH = 16
SWORD_HEIGHT = 16

class Player(MapEntity):
    moving = False
    running = False
    attacking = False
    
    run_timer = 0
    attack_timer = 0

    def Draw(self, xwin, ywin):
        super(Player, self).Draw(xwin, ywin)
        x = self.x - xwin
        y = self.y - ywin
        engine.font.Print(int(x), int(y), str(self.run_timer))

    def Update(self):
        if self.attacking:
            self.HandleAttack()
        super(Player, self).Update()

    def PerformAttack(self):
        if self.attack_timer == 0:
            self.attacking = True
            self.attack_timer = ATTACK_DURATION
            resources.player_attack_sound.Play()
            
    def HandleAttack(self):
        self.anim_state = "attack"
        
        if self.direction == direction.up:
            for entity in map.map_engine.GetEntitiesInRectangle(self, self.x, self.y - 20, self.x + self.hotspot_width, self.y):
                entity.Hurt(1)
        elif self.direction == direction.down:
            for entity in map.map_engine.GetEntitiesInRectangle(self, self.x, self.y + self.hotspot_height, self.x + self.hotspot_width, self.y + self.hotspot_height + 20):
                entity.Hurt(1)
        elif self.direction == direction.left:
            for entity in map.map_engine.GetEntitiesInRectangle(self, self.x - 20, self.y, self.x, self.y + self.hotspot_height):
                entity.Hurt(1)
        elif self.direction == direction.right:
            for entity in map.map_engine.GetEntitiesInRectangle(self, self.x + self.hotspot_width, self.y, self.x + self.hotspot_width + 20, self.y + self.hotspot_height):
                entity.Hurt(1)
            
        if self.attack_timer:
            self.attack_timer -= 1
            if not self.attack_timer:
                self.attacking = False
            
    def Input(self, held, pressed):
        self.moving = False
        move_speed = self.ika_entity.speed / 100.0
        
        self.x_speed = 0
        self.y_speed = 0

        if "quickmenu" in pressed:
            self.Hurt(0)

        # Trigger attack by pressing attack button. Yay.
        if "attack" in pressed:
            self.PerformAttack()
            
        # You can move forward while attacking, but can't run.
        # You move slower than you would normally. 
        if self.attacking:
            self.running = False
            self.run_timer = 0
            move_speed *= 0.375
        else:
            # Start running if you're pressing the button,
            # not already started and not recovering
            if "run" in pressed and not self.running and self.run_timer == 0:
                self.running = True
            # If you're already running, holding the button when moving
            # keeps it going
            elif "run" in held and self.running:
                self.running = True
            # Not running.
            else:
                self.running = False
        
        # Increase speed while running.
        if self.running:
            move_speed *= 3.0
        
        # Move up
        if "up" in held:
            self.direction = direction.up
            self.y_speed = -move_speed
            self.moving = True
        # Move down
        elif "down" in held:
            self.direction = direction.down
            self.y_speed = move_speed
            self.moving = True
            
        # Move left
        if "left" in held:
            self.direction = direction.left
            self.x_speed = -move_speed
            self.moving = True
        # Move right
        elif "right" in held:
            self.direction = direction.right
            self.x_speed = move_speed
            self.moving = True

        # If you let go of the run button, or stop moving while
        # holding the run button after already going,
        # force recovery before running can be done again.
        if not self.running or (self.running and not self.moving and self.run_timer > 0):
            self.running = False
            if self.run_timer > 0:
                self.run_timer = -RUN_RECOVERY_TIME
            elif self.run_timer < 0:
                self.run_timer += 1
            
        if self.moving:
            if self.running:
                self.anim_state = "run"

                self.run_timer += 1
                # After running for a certain length, you get tired out
                # and need to recover before you can run again.
                if self.run_timer == RUN_MAX_DURATION:
                    self.run_timer = -RUN_RECOVERY_TIME
                    self.running = False
            else:
                self.anim_state = "walk"
        else:
            self.anim_state = "idle"